$(function(){
	$(".mc").css("display","none");
	$('#categorys').hover(function(){
		$(this).children('.mc').toggle();
	});
	$Base.isLogin(location.href, getAddressList);
	$("#new_address_btn").click(function(){
		$('#add').show();
		$('#update').hide();
		$('#frm').attr("action","/json?action=ADD_T_DELIVERY_ADDRESS_ACTION");
		$('#frm').resetForm();
		$("#new_address").zxxbox({
			title:'新增地址',
			show : validation.clearTip()
		});
	 });
	siteSelect(["PROVINCE","CITY","COUNTY"],["","",""]);
});

var formCount = 0;
var defaultId;
function getAddressList(){
	var url = '/json?action=QUERY_T_DELIVERY_ADDRESS_ACTION';
	$.post(url,{},function(data){
		if(data.success){
			var listNode = $('#address_list');
			var arr = data.root.T_DELIVERY_ADDRESS.rs;
			listNode.html('');
			$.each(arr, function(i,o){
				var divStr = [
					'<div class="address mt15" onmouseover="setOverStyle('+i+')" onmouseout="setOutStyle('+i+')" id="list'+i+'" name="address">',
						'<h5>地址',i+1,
							'<span class="address_opr" id="oper'+i+'">',
								'<span class="operation" id="default'+i+'" onclick="setDefault('+o.DELIVERY_ADDRESS_ID+','+i+')">设为默认</span> ',
								'<span class="operation" id="update'+i+'" onclick="updateAddress('+o.DELIVERY_ADDRESS_ID+')">修改地址</span> ',
								'<span class="operation" id="delete'+i+'" onclick="deleteAddress('+o.DELIVERY_ADDRESS_ID+')">删除地址</span>',
							'</span>',
						'</h5>',
						'<ul>',
							'<li><label>收货人：</label><span>',o.CONSIGNEE_NAME,'</span></li>',
							'<li><label>详细地址：</label><span>',o.PROVINCE,'  ', o.CITY,'  ',o.COUNTY,'  ',o.DELIVERY_ADDRESS,'</span></li>',
							'<li><label>手机号码：</label><span>',o.MOBILE,'</span></li>',
							'<li><label>固定电话：</label><span>',o.TELEPHONE,'</span></li>',
							'<li><label>邮编：</label><span>',o.ZIP_CODE,'</span></li>',
						'</ul>',
					'</div>'
				].join('');
				listNode.append(divStr);
				formCount = i+1;
				if(o.IS_DEFAULT == 1){
					defaultId = i;
					$('#list'+i).addClass('address_current');
					$('#default'+i).hide();
					$('#delete'+i).hide();
				}
			});
			if(formCount == 10){
				$('#new_btn').hide();
			}
			$('#had_count').html(formCount);
			$('#over_count').html(10-formCount);
		}
	},'json');
}

function setOverStyle(id){
	$('#list'+id).addClass('address_bg');
	$('#oper'+id).show();
}

function setOutStyle(id){
	$('#list'+id).removeClass('address_bg');
	$('#oper'+id).hide();
}

function setDefault(addressId, id){
	var url = '/json?action=UPDATE_T_DELIVERY_ADDRESS_DEFAULT_ACTION&DELIVERY_ADDRESS_ID='+addressId;
	$.post(url,{},function(data){
		if(data.success){
			$('#list'+defaultId).removeClass('address_current');
			$('#default'+defaultId).show();
			$('#delete'+defaultId).show();
			defaultId = id;
			$('#list'+id).addClass('address_current');
			$('#default'+id).hide();
			$('#delete'+id).hide();
		}
	},'json');
}

function updateAddress(id){
	var url = '/json?action=GET_T_DELIVERY_ADDRESS_ACTION&DELIVERY_ADDRESS_ID='+id;
	$.post(url,{},function(data){
		if(data.success){
			$('#update').show();
			$('#add').hide();
			$('#frm').attr("action","/json?action=UPDATE_T_DELIVERY_ADDRESS_ACTION");
			var obj = data.root.T_DELIVERY_ADDRESS.rs[0];
			$('#DELIVERY_ADDRESS_ID').val(obj.DELIVERY_ADDRESS_ID);
			$('#CONSIGNEE_NAME').val(obj.CONSIGNEE_NAME);
			siteSelect(["PROVINCE","CITY","COUNTY"],[obj.PROVINCE,obj.CITY,obj.COUNTY]);
			$('#DELIVERY_ADDRESS').val(obj.DELIVERY_ADDRESS);
			$('#MOBILE').val(obj.MOBILE);
			$('#TELEPHONE').val(obj.TELEPHONE);
			$('#ZIP_CODE').val(obj.ZIP_CODE);
			$("#new_address").zxxbox({
				title:'修改地址',
				show : validation.clearTip()
			});
		}
	},'json');
}

function deleteAddress(id){
	if(!confirm("确定删除这个收货地址吗？")){
		return;
	}
	var url = '/json?action=DELETE_T_DELIVERY_ADDRESS_ACTION&DELIVERY_ADDRESS_ID='+id;
	$.post(url,{},function(data){
		if(data.success){
			getAddressList();
		}
	},'json');
}

var validation = {
	
	checkName: function(){
		var nameObj = $('#CONSIGNEE_NAME');
		$('#CONSIGNEE_NAME_TIP').html('');
		if($.trim(nameObj.val()).length == 0){
			$('#CONSIGNEE_NAME_TIP').html('收货人必填');
			return false;
		}
		return true;
	},
	
	checkPCC: function(){
		var province = $('#PROVINCE').val();
		var city = $('#CITY').val();
		var county = $('#COUNTY').val();
		$('#ADDRESS_TIP').html('');
		if(province.length == 0 || city.length == 0 || county.length ==0){
			$('#ADDRESS_TIP').html('请选择完整的地区信息');
			return false;
		}
		return true;
	}, 
	
	checkAddress: function(){
		var addressObj = $('#DELIVERY_ADDRESS');
		$('#DELIVERY_ADDRESS_TIP').html('');
		if($.trim(addressObj.val()).length == 0){
			$('#DELIVERY_ADDRESS_TIP').html('请填写真实的详细地址，方便送货人员能快速送货上门');
			return false;
		}
		return true;
	},
	
	checkContact: function(){
		var mobileObj = $('#MOBILE');
		var telephoneObj = $('#TELEPHONE');
		$('#MOBILE_TIP').html('');
		if($.trim(mobileObj.val()).length == 0 && $.trim(telephoneObj.val()).length == 0){
			$('#MOBILE_TIP').html('手机号码和固定电话必须填写一个，方便送货人员送货时联系您');
			return false;
		}
		return true;
	},
	
	checkAll: function(){
		return validation.checkName() && validation.checkPCC() && validation.checkAddress() && validation.checkContact();
	},
	
	clearTip: function(){
		$('#CONSIGNEE_NAME_TIP').html('');
		$('#ADDRESS_TIP').html('');
		$('#DELIVERY_ADDRESS_TIP').html('');
		$('#MOBILE_TIP').html('');
	}
}

function saveAddress(){
	if(!validation.checkAll()){
		return;
	}
	
	var options = {
	    dataType: 'json',
    	iframe:	false,
	    success: function(data) {
	    	if(data.success){
	    		$('#frm').resetForm();
	    		$.zxxbox.hide();
	    		getAddressList();
	      	}else{
	      		alert(data.errors.errmsg);
	      	}
	   	}
	};
	$.ajaxSetup({contentType:'application/x-www-form-urlencoded;charset=utf-8'});
    $('#frm').ajaxSubmit(options);
}

